using System;
using System.Data;
using System.Xml;

namespace Xajhzwb.Components.Data
{
    public abstract class AbstractDataAccess : DataAccess
    {
        #region DataAccess

        #region Support Property & method

        public abstract DatabaseType DatabaseType { get; }
        public abstract IDbConnection DbConnection { get; }

        public void Close()
        {
            this.DbConnection.Close();
        }

        public void Open()
        {
            if(this.DbConnection.State.Equals(ConnectionState.Closed))
                this.DbConnection.Open();
        }

        public bool IsClosed
        {
            get
            {
                return this.DbConnection.State.Equals(ConnectionState.Closed);
            }
        }

        public abstract IDbTransaction BeginTransaction();

        #endregion Support Property & method

        #region ExecuteNonQuery

        public int ExecuteNonQuery(CommandType commandType, string commandText)
        {
            return this.ExecuteNonQuery(commandType, commandText, null);
        }

        public int ExecuteNonQuery(string commandText)
        {
            return this.ExecuteNonQuery(CommandType.Text, commandText, null);
        }

        public int ExecuteNonQuery(string commandText, QueryParameterCollection commandParameters)
        {
            return this.ExecuteNonQuery(CommandType.Text, commandText, commandParameters);
        }

        public abstract int ExecuteNonQuery(CommandType commandType, string commandText, QueryParameterCollection commandParameters);

        #endregion ExecuteNonQuery

        #region ExecuteDataSet

        public DataSet ExecuteDataset(CommandType commandType, string commandText)
        {
            return ExecuteDataset(commandType, commandText, null, new DataSet(), null);
        }

        public DataSet ExecuteDataset(string commandText)
        {
            return ExecuteDataset(CommandType.Text, commandText, null, new DataSet(), null);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteDataset(commandType, commandText, commandParameters, new DataSet(), null);
        }

        public DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteDataset(CommandType.Text, commandText, commandParameters, new DataSet(), null);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, DataSet ds)
        {
            return ExecuteDataset(commandType, commandText, null, ds, null);
        }

        public DataSet ExecuteDataset(string commandText, DataSet ds)
        {
            return ExecuteDataset(CommandType.Text, commandText, null, ds, null);
        }

        public DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, DataSet ds)
        {
            return ExecuteDataset(CommandType.Text, commandText, commandParameters, ds, null);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, string tableName)
        {
            return ExecuteDataset(commandType, commandText, null, new DataSet(), tableName);
        }

        public DataSet ExecuteDataset(string commandText, string tableName)
        {
            return ExecuteDataset(CommandType.Text, commandText, null, new DataSet(), tableName);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters, string tableName)
        {
            return ExecuteDataset(commandType, commandText, commandParameters, new DataSet(), tableName);
        }

        public DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, string tableName)
        {
            return ExecuteDataset(CommandType.Text, commandText, commandParameters, new DataSet(), tableName);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, DataSet ds, string tableName)
        {
            return ExecuteDataset(commandType, commandText, null, ds, tableName);
        }

        public DataSet ExecuteDataset(string commandText, DataSet ds, string tableName)
        {
            return ExecuteDataset(CommandType.Text, commandText, null, ds, tableName);
        }

        public DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, DataSet ds, string tableName)
        {
            return ExecuteDataset(CommandType.Text, commandText, commandParameters, ds, tableName);
        }

        public DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters, DataSet ds)
        {
            return ExecuteDataset(CommandType.Text, commandText, commandParameters, ds, null);
        }

        public abstract DataSet ExecuteDataset(CommandType commandType, string commandText, QueryParameterCollection commandParameters, DataSet ds, string tableName);

        #endregion ExecuteDataSet;

        #region ExecuteReader

        public IDataReader ExecuteReader(CommandType commandType, string commandText)
        {
            return ExecuteReader(commandType, commandText, null);
        }

        public IDataReader ExecuteReader(string commandText)
        {
            return ExecuteReader(CommandType.Text, commandText, null);
        }

        public IDataReader ExecuteReader(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteReader(CommandType.Text, commandText, commandParameters);
        }

        public abstract IDataReader ExecuteReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters);

        #endregion ExecuteReader

        #region ExecuteScalar

        public object ExecuteScalar(CommandType commandType, string commandText)
        {
            return ExecuteScalar(commandType, commandText, null);
        }

        public object ExecuteScalar(string commandText)
        {
            return ExecuteScalar(CommandType.Text, commandText, null);
        }

        public object ExecuteScalar(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteScalar(CommandType.Text, commandText, commandParameters);
        }

        public abstract object ExecuteScalar(CommandType commandType, string commandText, QueryParameterCollection commandParameters);

        #endregion ExecuteScalar

        #region ExecuteXmlReader

        public XmlReader ExecuteXmlReader(CommandType commandType, string commandText)
        {
            return ExecuteXmlReader(commandType, commandText, null);
        }

        public XmlReader ExecuteXmlReader(string commandText)
        {
            return ExecuteXmlReader(CommandType.Text, commandText, null);
        }

        public XmlReader ExecuteXmlReader(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteXmlReader(CommandType.Text, commandText, commandParameters);
        }

        public abstract XmlReader ExecuteXmlReader(CommandType commandType, string commandText, QueryParameterCollection commandParameters);

        #endregion ExecuteXmlReader

        #endregion

        protected void SyncParameter(QueryParameterCollection commandParameters)
        {
            if((commandParameters != null) && (commandParameters.Count > 0))
            {
                for(int i = 0; i < commandParameters.Count; i++)
                {
                    commandParameters[i].SyncParameter();
                }
            }
        }
    }
}

